﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Jy123.Web.Manage.User
{
    public partial class UserEdit : MgConfig//System.Web.UI.Page
    {
        protected string pid,cid;
        Model.User_Main modelUser = new Jy123.Model.User_Main();
        BLL.User_Main bllUser = new Jy123.BLL.User_Main();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("User/index.aspx");
            if (!IsPostBack)
            {
               ShowUserInfo();
            }
            
        }
        /// <summary>
        /// 加载显示用户信息
        /// </summary>
        private void ShowUserInfo()
        {
            //Model.User_Main modelUser = new Jy123.Model.User_Main();
            //BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            modelUser = bllUser.GetModel(Convert.ToInt32(Request.QueryString["UserID"]));//测试用户，写死。到时候换回来
            txtName.Text = modelUser.UserName;
            txtEmail.Text = modelUser.Email;

            //BLL.User_Dict_Area bllArea = new Jy123.BLL.User_Dict_Area();
            //ddlProvince.DataSource = bllArea.GetModel(modelUser.Province);
            //Common.Public.GetSelect(selProvince, modelUser.Province.ToString(), true);
            //Common.Public.GetSelect(selCity, modelUser.City.ToString(), true);

            pid = modelUser.Province.ToString();
            cid = modelUser.City.Value.ToString();

            radState.SelectedValue = modelUser.State.ToString();
            labLoginNumber.Text = modelUser.LoginTimes.ToString();
            labLastLoginTime.Text = modelUser.LastLoginTime.ToString();
            labLastLoginIP.Text = modelUser.LastLoginIP;
            labregTime.Text = modelUser.RegTime.ToString();
            labRegIP.Text = modelUser.RegIP;
            txtCash.Text = modelUser.Cash.ToString();
            txtClick.Text = modelUser.Click.ToString();
            labInviterID.Text = modelUser.InviterID.ToString();
            //ScriptManager.RegisterStartupScript(this, typeof(Page), "SetSelectValue", "SetSelectValue()",true);
        }

        //保存用户信息
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Model.User_Main modelUser = new Jy123.Model.User_Main();
            //BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            modelUser = bllUser.GetModel(Convert.ToInt32(Request.QueryString["UserID"]));//测试用户，写死。到时候换回来

            
            if (!string.IsNullOrEmpty(txtPwd.Text))//密码不为空
            {
                modelUser.UserName = txtName.Text;
                modelUser.Email = txtEmail.Text;
                modelUser.Password = Common.Input.MD5(txtPwd.Text, false);
                modelUser.Province = int.Parse(Request.Form["selProvince"]);
                modelUser.City = int.Parse(Request.Form["selCity"]);
                modelUser.State = Convert.ToInt32(radState.SelectedValue);
                modelUser.Cash = Convert.ToInt32(txtCash.Text);
                modelUser.Click = Convert.ToInt32(txtClick.Text);
                
                if (!string.IsNullOrEmpty(txtName.Text)
                || !string.IsNullOrEmpty(txtEmail.Text)
                || !string.IsNullOrEmpty(Request.Form["selProvince"].ToString())
                || !string.IsNullOrEmpty(Request.Form["selCity"].ToString())
                || !string.IsNullOrEmpty(radState.SelectedValue)
                || !string.IsNullOrEmpty(txtCash.Text)
                || !string.IsNullOrEmpty(txtClick.Text))
                {
                    bllUser.Update(modelUser);
                    ShowUserInfo();
                }
                else
                {
                    //Response.Write("<script language='javascript'>alert('');</script>");
                    MessageBox("有空格未填", MsgType.warning);
                }
            }
            else//密码为空
            {
                modelUser.UserName = txtName.Text;
                modelUser.Email = txtEmail.Text;

                modelUser.Province = int.Parse(Request.Form["selProvince"]);
                modelUser.City = int.Parse(Request.Form["selCity"]);
                modelUser.State = Convert.ToInt32(radState.SelectedValue);
                modelUser.Cash = Convert.ToInt32(txtCash.Text);
                modelUser.Click = Convert.ToInt32(txtClick.Text);
                bllUser.Update(modelUser);
                ShowUserInfo();
                //Response.Write("<script language='javascript'>alert('');</script>");
                MessageBox("修改成功", MsgType.ok);
            }
            
            
        }


    }
}
